const {ENUM_CACHE_TYPE} = require("../../../static/enum.js");
const Validator = require("../../validator/index.js");
class CacheConfigItem {
    constructor(param) {
        const {
            key,// 缓存的key
            type = ENUM_CACHE_TYPE.MEMORY.value, // 缓存的类型 "storage" storage缓存 | "memory" 内存缓存
            expireTime = 0,// 缓存过期时间 单位毫秒 0或空为永不过期
        } = param || {}
        this.key = key;
        this.type = type;
        this.expireTime = expireTime;
        valid.call(this);
    }
}

function valid() {
    let validator = new Validator();
    validator.add(this.key, "isNotEmpty", "cache config item key should not be empty");
        const validTypes = Object.values(ENUM_CACHE_TYPE).map(item=>{return item.value});
    validator.add(validTypes.includes(this.type), "isTrue", `cache config item which key ${this.key} type is invaild` );
    let errMsg = validator.start();
    if (errMsg) throw new Error(errMsg);
}

module.exports = CacheConfigItem;